package com.moyun.search.task;

import com.moyun.search.common.utils.FileUtils;
import com.moyun.search.dao.repository.ProjectFileLabRepository;
import com.moyun.search.domain.FileLab;
import com.moyun.search.domain.ProjectAttachment;
import com.moyun.search.domain.ProjectInfo;
import org.joda.time.DateTime;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件同步到ES服务任务
 * @author: zealon
 * @since: 2021/12/22
 */
public class FileSyncElasticSearchTask implements Runnable {

    private MultipartFile mulfile;
    private ProjectAttachment attachment;
    private ProjectFileLabRepository projectFileLabRepository;
    private ProjectInfo project;

    @Override
    public void run() {
        String fileBase64 = FileUtils.encodeBase64MultipartFile(this.mulfile);
        FileLab fileLab = new FileLab();
        fileLab.setId(attachment.getId());
        fileLab.setCreate_time(DateTime.now().toString("yyyy-MM-dd hh:mm:ss"));
        fileLab.setData(fileBase64);
        fileLab.setFile_name(attachment.getFileName());
        fileLab.setFile_type(attachment.getFileType());
        fileLab.setProject_id(project.getId());
        fileLab.setProject_name(project.getProjectName());
        this.projectFileLabRepository.saveDocument(fileLab);
    }

    public FileSyncElasticSearchTask(ProjectFileLabRepository projectFileLabRepository, MultipartFile mulfile,
                                     ProjectAttachment attachment, ProjectInfo project) {
        this.projectFileLabRepository = projectFileLabRepository;
        this.mulfile = mulfile;
        this.attachment = attachment;
        this.project = project;
    }
}
